package com.techwells.applicationMarket.service.impl;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.techwells.applicationMarket.dao.AppMapper;
import com.techwells.applicationMarket.dao.SystemConfigMapper;
import com.techwells.applicationMarket.dao.TaskMapper;
import com.techwells.applicationMarket.dao.TaskTypeMapper;
import com.techwells.applicationMarket.dao.UserMapper;
import com.techwells.applicationMarket.dao.UserTaskCountMapper;
import com.techwells.applicationMarket.dao.UserTaskMapper;
import com.techwells.applicationMarket.dao.WalletDetailMapper;
import com.techwells.applicationMarket.dao.WalletMapper;
import com.techwells.applicationMarket.domain.SystemConfig;
import com.techwells.applicationMarket.domain.Task;
import com.techwells.applicationMarket.domain.TaskType;
import com.techwells.applicationMarket.domain.User;
import com.techwells.applicationMarket.domain.UserTask;
import com.techwells.applicationMarket.domain.UserTaskCount;
import com.techwells.applicationMarket.domain.UserTaskCountKey;
import com.techwells.applicationMarket.domain.Wallet;
import com.techwells.applicationMarket.domain.WalletDetail;
import com.techwells.applicationMarket.domain.rs.AppVersionImageVos;
import com.techwells.applicationMarket.domain.rs.TaskAdminVos;
import com.techwells.applicationMarket.domain.rs.TaskAppVersionVos;
import com.techwells.applicationMarket.domain.rs.userTaskDetailVos;
import com.techwells.applicationMarket.exception.GlobalException;
import com.techwells.applicationMarket.service.TaskService;
import com.techwells.applicationMarket.util.Base64Util;
import com.techwells.applicationMarket.util.DateUtil;
import com.techwells.applicationMarket.util.PagingTool;
import com.techwells.applicationMarket.util.ResultInfo;
import com.techwells.applicationMarket.util.swtc.Amount;
import com.techwells.applicationMarket.util.swtc.PayNeedData;
import com.techwells.applicationMarket.util.swtc.PayObject;
import com.techwells.applicationMarket.util.swtc.SwtcUtils;

@Service
public class TaskServiceImpl implements TaskService{
	
	private Logger logger=LoggerFactory.getLogger(TaskServiceImpl.class);  //日志记录
	
	
	@Resource
	private TaskMapper taskMapper;
	
	@Resource
	private UserTaskMapper userTaskMapper;
	
	@Resource
	private WalletDetailMapper detailMapper;
	
	
	@Resource
	private AppMapper appMapper;
	@Resource
	private UserMapper userMapper;
	@Resource
	private WalletMapper walletMapper;
	
	@Resource
	private SystemConfigMapper configMapper;
	
	@Resource
	private TaskTypeMapper taskTypeMapper;
	
	@Resource
	private UserTaskCountMapper userTaskCountMapper;
	
	
	@Override
	public Object addTask(Task task) throws Exception {
		ResultInfo resultInfo=new ResultInfo();
		int count=taskMapper.insertSelective(task);
		
		if (count==0) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("添加失败");
		}
		
		resultInfo.setMessage("添加成功");
		return resultInfo;
	}

	@Override
	public Object getTask(Integer taskId) throws Exception {
		ResultInfo resultInfo=new ResultInfo();
		Task task=taskMapper.selectByPrimaryKey(taskId);
		if (task==null) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("该应用不存在");
			return resultInfo;
		}
		
		//获取任务完成的次数
		int total = userTaskMapper.countTotalByTaskId(taskId);
		Map<String, Object> map=new HashMap<>();
		map.put("total", total);
		map.put("task", task);
		
		resultInfo.setResult(map);
		resultInfo.setMessage("获取成功");
		return resultInfo;
	}

	
	@Override
	public Object deleteTask(Integer taskId) throws Exception {
		ResultInfo resultInfo=new ResultInfo();
		
		if (taskId==10||taskId==12) {
			resultInfo.setMessage("删除成功");
			return resultInfo;
		}
		
		int count=taskMapper.deleteByPrimaryKey(taskId);
		
		if (count==0) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("删除失败");
			return resultInfo;
		}
		
		
		resultInfo.setMessage("删除成功");
		return resultInfo;
	}

	@Override
	public Object modifyTask(Task task) throws Exception {
		ResultInfo resultInfo=new ResultInfo();
		int count=taskMapper.updateByPrimaryKeySelective(task);
		if (count==0) {
			resultInfo.setMessage("失败");
			resultInfo.setCode("-1");
			return resultInfo;
		}
		resultInfo.setMessage("成功");
		return resultInfo;
	}

	@Override
	public Object getTaskTypeList() throws Exception {
		ResultInfo resultInfo=new ResultInfo();
		List<TaskType> taskTypes=taskTypeMapper.selectTaskTypes();
		resultInfo.setMessage("获取成功");
		resultInfo.setResult(taskTypes);
		return resultInfo;
	}

	@Override
	public Object getTaskList(PagingTool pagingTool) {
		ResultInfo resultInfo=new ResultInfo();
		int count=taskMapper.countTotal(pagingTool);
		List<TaskAdminVos> taskAdminVos=taskMapper.selectTaskList(pagingTool);
		resultInfo.setTotal(count);
		resultInfo.setResult(taskAdminVos);
		resultInfo.setMessage("获取成功");
		return resultInfo;
	}

	@Override
	public Object getDetailList(PagingTool pagingTool) throws Exception {
		ResultInfo resultInfo=new ResultInfo();
		
		//分页获取任务
		int total=taskMapper.countTotalDetail(pagingTool);
		
		//分页获取的任务信息，有效的、已开始的，未结束的，                                                        ----当前用户未完成的或者未达到允许完成次数的，未完待续-----
		List<Task> tasks=taskMapper.selectTaskDetailList(pagingTool);
		
		List<TaskAppVersionVos> taskAppVersionVos=new ArrayList<TaskAppVersionVos>();   //封装任务信息
		
		//遍历获取详细信息
		for (Task task : tasks) {
			//根据用户Id和任务Id获取用户所有完成的任务
//			List<UserTask> userTaskList=userTaskMapper.selectUserTasksListByUserIdAndTaskId((Integer)pagingTool.getParams().get("userId"),task.getTaskId());   //根据用户信息查找
////			
////			//如果userTask存在，并且已经达到了允许下载的次数了，那么就不能显示在任务中给用户
//			if (userTaskList!=null&&userTaskList.size()>=task.getAllowNumber()) {
//				continue;   //继续下一次的循环，不返回
//			}
			
			//根据用户Id和任务Id获取用户完成任务的信息
			
			UserTaskCountKey userTaskCountKey=new UserTaskCountKey();
			userTaskCountKey.setUserId((Integer)pagingTool.getParams().get("userId"));
			userTaskCountKey.setTaskId(task.getTaskId());
			UserTaskCount userTaskCount = userTaskCountMapper.selectByPrimaryKey(userTaskCountKey);
			
			if (userTaskCount!=null&&userTaskCount.getCount()>=task.getAllowNumber()) {//如果完成次数已经达到了最大完成的次数
				continue;   //继续下一次的循环，不返回
			}
			
			TaskAppVersionVos taskApp=null;
			//如果是下载安装类，需要联合查询应用信息
			if (task.getTaskTypeId().equals(1)) {
				//查询应用的信息
				AppVersionImageVos appVersion=appMapper.selectAppVersionImageVos(task.getAppId(),(Integer)pagingTool.getParams().get("platform"));
				if (appVersion!=null) {
					taskApp=new TaskAppVersionVos();
					taskApp.setAppId(task.getAppId());  //应用Id
					taskApp.setTaskId(task.getTaskId());
					taskApp.setAppName(appVersion.getAppName());  //应用名称
					taskApp.setLogo(appVersion.getLogo());  //图标 
					taskApp.setVersionNum(appVersion.getVersionNum()); //版本号
					taskApp.setSize(appVersion.getSize());   //应用大小
					taskApp.setIntroduction(task.getIntroduction());//简介
					taskApp.setRewardMoney(task.getRewardMoney());  //奖励的钱
					taskApp.setActivated(task.getActivated());   //奖励的类型
					taskApp.setTaskTypeId(task.getTaskTypeId());  //任务类型的Id
					taskApp.setTaskName(task.getTaskName());
					taskApp.setPackageName(appVersion.getPackageName());
					taskApp.setDownloadUrl(appVersion.getDownloadUrl());
					taskApp.setDownloadCount(appVersion.getDownloadCount());
				}
			}
//			else {   //如果是其他类型的，直接返回信息即可
//				taskApp=new TaskAppVersionVos();
//				taskApp.setIntroduction(task.getIntroduction());//简介
//				taskApp.setRewardMoney(task.getRewardMoney());  //奖励的钱
//				taskApp.setActivated(task.getActivated());   //奖励的类型
//				taskApp.setLink(task.getLink());  //链接
//				taskApp.setTaskTypeId(task.getTaskTypeId());  //任务类型的Id
//				taskApp.setTaskName(task.getTaskName());
//				taskApp.setTaskId(task.getTaskId());
//				taskApp.setLogo("http://www.emoonbow.com/applicationMarket-upload/appType/dpplog.png");  //其他类型的都是使用默认的图片
//			}
			if (taskApp!=null) {
				taskAppVersionVos.add(taskApp);  //添加到集合中
			}
		}
		
		
		//获取当前用户的任务完成情况
		List<UserTask> userTasks=userTaskMapper.selectUserTaskList((Integer)pagingTool.getParams().get("userId"));
		
		Map<String, Object> map=new HashMap<String, Object>();  //封装结果集
		
		int type=(int) pagingTool.getParams().get("type");  //任务的类型
		
		if (type==1) {  //下载任务
			resultInfo.setTotal(total);
			resultInfo.setResult(taskAppVersionVos.size()!=0?taskAppVersionVos:null);
			resultInfo.setMessage("获取完成");
			return resultInfo;
		}else {  //已完成的任务
			resultInfo.setTotal(total);
			resultInfo.setResult(userTasks.size()>0?userTasks:null);
			resultInfo.setMessage("获取完成");
			return resultInfo;
		}
	}

	
	/**
	 * 领取任务奖励
	 * @throws Throwable 
	 */
	@Override
	public Object receiveReward(Integer taskDetailId,String hash) throws Throwable {
		ResultInfo resultInfo=new ResultInfo();
		
		//根据id查询信息
		UserTask userTask=userTaskMapper.selectByPrimaryKey(taskDetailId);
		
		if (userTask==null) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("该任务明细不存在");
			return resultInfo;
		}
		
		if (userTask.getStatus().equals(1)) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("该任务奖励已发放");
			return resultInfo;
		}
		
		
		//获取配置信息，其中包含开发商的账号和秘钥
		SystemConfig config=configMapper.selectByPrimaryKey(1);  
		
		if (config==null) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("配置信息不存在");
			return resultInfo;
		}
		
		
		
		//未发放，存在，那么就需要进行转账了
		
		if (userTask.getActivated().equals(1)) {  //如果是MOAC，前台进行转账，这里改变下状态即可

			if (StringUtils.isEmpty(hash)) {
				resultInfo.setCode("-1");
				resultInfo.setMessage("hash不能为空");
				return resultInfo;
			}
			
			//开发商只有配置了账号才能进行转账
//			if (config.getSwtcAddress()==null||config.getSwtcSecret()==null) {
//				resultInfo.setCode("-1");
//				resultInfo.setMessage("开发商没有配置账号，暂时不能领取奖励");
//				return resultInfo;
//			}
//			
//			//根据用户Id和对应的钱包类型获取用户的钱包信息,2表示井通钱包 1 表示MOAC钱包
//			Wallet wallet=walletMapper.selectWallet(userTask.getUserId(),1);
//			
//			if (wallet==null) {
//				resultInfo.setCode("999999");   //特殊的返回码，前台需要定制
//				resultInfo.setMessage("您还没有导入钱包账户，请导入井通钱包账户");
//				return resultInfo;
//			}
			
			
			//根据用户Id获取moac钱包
			Wallet wallet=walletMapper.selectWallet(userTask.getUserId(), 1);
			
			if (wallet==null) {
				resultInfo.setCode("999999");   //特殊的返回码，前台需要定制
				resultInfo.setMessage("您还没有导入钱包账户，请导入井通钱包账户");
				return resultInfo;
			}
			
			//由于交易延迟的问题，因此先保留交易的hash
			WalletDetail detail=new WalletDetail();
			//墨客转账是在前台完成的，这里只需要用hash值查询转账的信息即可
//			TransactionDetail transactionDetail=MoacUtils.getTransactionDetail(hash);
//			detail.setNumber(System.currentTimeMillis()+"");
			detail.setCreateDate(new Date());  //设置创建日期
//			detail.setBlock(transactionDetail.getBlockNumber());   //设置区块信息
//			detail.setFromAddress(transactionDetail.getFrom());  //转账方的钱包地址
//			detail.setToAddress(transactionDetail.getTo());
//			detail.setFee((Double)(Long.parseLong(transactionDetail.getGas())/1000000000000000000.0*Long.parseLong(transactionDetail.getGasPrice())));   //旷工费用
			detail.setRemark("任务奖励发放");   //设置备注为转账
			detail.setMoney("+"+userTask.getMoney());
//			Date trsdate=new Date();   //交易日期
//			detail.setTransactionDate(DateUtil.getDate("yyyy-MM-dd HH:mm:ss"));  //交易时间
			detail.setActivated(userTask.getActivated());  //设置钱包的类型
//			detail.setMoney("-"+transactionDetail.getValue());
			detail.setWalletId(wallet.getWalletId());
			detail.setHash(hash);
			
			int count=detailMapper.insertSelective(detail);
			if (count==0) {
				throw new RuntimeException();
			}
			
		}else if(userTask.getActivated().equals(2)){  //如果是井通
			//开发商只有配置了账号才能进行转账
			if (config.getSwtcAddress()==null||config.getSwtcSecret()==null) {
				resultInfo.setCode("-1");
				resultInfo.setMessage("开发商没有配置账号，暂时不能领取奖励");
				return resultInfo;
			}
			
			//根据用户Id和对应的钱包类型获取用户的钱包信息,2表示井通钱包
			Wallet wallet=walletMapper.selectWallet(userTask.getUserId(),2);
			
			if (wallet==null) {
				resultInfo.setCode("999999");   //特殊的返回码，前台需要定制
				resultInfo.setMessage("您还没有导入钱包账户，请导入井通钱包账户");
				return resultInfo;
			}
			
			//钱包存在，配置存在，那么可以进行转账了
			PayNeedData data=new PayNeedData();
			String orderNum=System.currentTimeMillis()+"";
			data.setClient_id(orderNum);  //订单号
			data.setSecret(Base64Util.Decoder(config.getSwtcSecret()));  //秘钥
			
			PayObject payObject=new PayObject();
			payObject.setDestination(wallet.getAddress());   //目标账号
			payObject.setSource(config.getSwtcAddress());  //发起账号
			
			//转账的金额
			Amount amount=new Amount();
			amount.setValue(String.valueOf(userTask.getMoney()));  //设置转账的金额
			
			payObject.setAmount(amount);
			
			data.setPayment(payObject);
			
			
			//调用转账接口
			Map<String, Object> map=SwtcUtils.pay(data, config.getSwtcAddress());
			
			//转账失败
			if (map==null) {
				resultInfo.setCode("-1");
				resultInfo.setMessage("领取失败");
				return resultInfo;
			}
			
			if (!(Boolean)map.get("success")==true||!(map.get("result").equals("tesSUCCESS"))) {
				resultInfo.setCode("-1");
				resultInfo.setMessage("领取失败");
				return resultInfo;
			}
			
			String swtHash=(String) map.get("hash");  //获取hash值，用于查询交易信息
			
			//添加一条钱包明细
			//封装信息
			WalletDetail detail=new WalletDetail();
			detail.setHash(swtHash);  //hash的值
			detail.setNumber(orderNum);   //交易号
			detail.setCreateDate(new Date());  //设置创建日期
			detail.setBlock(null);   //设置区块信息
			detail.setFromAddress(config.getSwtcAddress());  //转账方的钱包地址
			detail.setToAddress(wallet.getAddress());
			detail.setFee((Double)map.get("fee"));   //旷工费用
			detail.setRemark("任务奖励发放");   //设置备注为转账
			Date trsdate=new Date();   //交易日期
			detail.setTransactionDate(DateUtil.getDate("yyyy-MM-dd HH:mm:ss"));  //交易时间
			detail.setUrl(SwtcUtils.HTTP+"v2/transactions/"+hash);   //设置公开查账的地址
			detail.setActivated(wallet.getType());  //设置钱包的类型
			detail.setMoney("+"+userTask.getMoney());
			detail.setWalletId(wallet.getWalletId());
			
			int count=detailMapper.insertSelective(detail);
			if (count==0) {
				throw new RuntimeException();
			}
			
		}
		
		//更新信息为已领取
		userTask.setStatus(1);
		
		int count=userTaskMapper.updateByPrimaryKeySelective(userTask);
		
		if (count==0) {
			throw new RuntimeException();
		}
		
		resultInfo.setMessage("领取成功");
		return resultInfo;
	}

	/**
	 * 验证用户完成的次数、失效、开始时间和结束时间
	 */
	@Override
	public Object finishTask(Integer userId, Integer taskId) throws Exception {
		ResultInfo resultInfo=new ResultInfo();
		
		//获取任务信息
		Task task=taskMapper.selectByPrimaryKey(taskId);
		
		if (task==null) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("该任务不存在");
			return resultInfo;
		}
		
		if (task.getStatus().equals(0)) {   //失效
			resultInfo.setCode("-1");
			resultInfo.setMessage("任务已经失效");
			return resultInfo;
		}
		
		
		//验证时间
		if (!(task.getStartDate().getTime()<System.currentTimeMillis()&&System.currentTimeMillis()<task.getEndDate().getTime())) { 
			resultInfo.setCode("-1");
			resultInfo.setMessage("任务还未开始或者已经结束");
			return resultInfo;
		}
		
		
		//根据用户Id和任务Id获取用户完成任务的信息
		
		UserTaskCountKey userTaskCountKey=new UserTaskCountKey();
		userTaskCountKey.setUserId(userId);
		userTaskCountKey.setTaskId(taskId);
		UserTaskCount userTaskCount = userTaskCountMapper.selectByPrimaryKey(userTaskCountKey);
		
		if (userTaskCount!=null&&userTaskCount.getCount()>=task.getAllowNumber()) {//如果完成次数已经达到了最大完成的次数
			resultInfo.setCode("-1");
			resultInfo.setMessage("已经达到最大完成次数");
			return resultInfo;
		}
		
		//插入完成的记录次数
		if (userTaskCount==null) {
			userTaskCount=new UserTaskCount();
			userTaskCount.setCount(1);
			userTaskCount.setUserId(userId);
			userTaskCount.setTaskId(taskId);
			int count1=userTaskCountMapper.insertSelective(userTaskCount);
			if (count1==0) {
				resultInfo.setCode("-1");
				resultInfo.setMessage("添加任务记录失败");
				return resultInfo;
			}
		}else {  //如果不为空，只是记录没有达到
			userTaskCount.setCount(userTaskCount.getCount()+1);
			int count1=userTaskCountMapper.updateByPrimaryKeySelective(userTaskCount);
			if (count1==0) {
				resultInfo.setCode("-1");
				resultInfo.setMessage("添加任务记录失败");
				return resultInfo;
			}
		}
		
		
		
//		List<UserTask> userTasks=userTaskMapper.selectUserTasksListByUserIdAndTaskId(userId, taskId);
//		
//		if (userTasks.size()>=task.getAllowNumber()) {   //如果完成次数已经达到了最大完成的次数
//			resultInfo.setCode("-1");
//			resultInfo.setMessage("已经达到最大完成次数");
//			return resultInfo;
//		}
		
		//没有达到最大完成次数，那么直接添加一条记录即可
		UserTask userTask=new UserTask();
		userTask.setCreateDate(new Date());
		userTask.setStatus(3);  //未审核
		userTask.setUserId(userId);
		userTask.setTaskId(taskId);
		userTask.setActivated(task.getActivated());  //奖励金额的类型
		userTask.setMoney(task.getRewardMoney());  //奖励的钱
		
		String content=null;
		if (task.getActivated().equals(1)) {  //领取墨客币
			content=task.getTaskName();
		}else {  //井通
			content=task.getTaskName();
		}
		
		userTask.setContent(content);
		
		//插入
		int count=userTaskMapper.insertSelective(userTask);
		
		if (count==0) {
			throw new RuntimeException();
		}
		
		resultInfo.setMessage("任务完成成功");
		return resultInfo;
	}

	@Override
	public Object deleteAlreadyFinishTask(Integer taskDetailId)
			throws Exception {
		ResultInfo resultInfo=new ResultInfo();	
		UserTask userTask = userTaskMapper.selectByPrimaryKey(taskDetailId);
		if (userTask==null) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("该奖励信息不存在");
			return resultInfo;
		}
		
		if (userTask.getStatus().equals(2)) {  //待发放
			resultInfo.setCode("-1");
			resultInfo.setMessage("只能删除已发放的奖励");
			return resultInfo;
		}
		
		int count=userTaskMapper.deleteByPrimaryKey(taskDetailId);
		
		if (count==0) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("删除失败");
			return resultInfo;
		}
		resultInfo.setMessage("删除成功");
		return resultInfo;
	}

	@Override
	public Object getFinshTaskList(PagingTool pagingTool) throws Exception {
		ResultInfo resultInfo=new ResultInfo();
		int total = userTaskMapper.countTotalUserTaskDetailVos(pagingTool);
		if (total==0) {
			resultInfo.setMessage("获取成功");
			resultInfo.setTotal(total);
			resultInfo.setResult(null);
			return resultInfo;
		}
		
		List<userTaskDetailVos> userTaskDetailVos = userTaskMapper.selectUserTaskDetailVos(pagingTool);
		resultInfo.setMessage("获取成功");
		resultInfo.setTotal(total);
		resultInfo.setResult(userTaskDetailVos);
		return resultInfo;
	}

	@Override
	public Object handlerTask(String[] ids, Integer status) {
		ResultInfo resultInfo = new ResultInfo();
		
		
		if (status.equals(1)) {  //奖励发放
			//获取系统配置
			SystemConfig config = configMapper.selectByPrimaryKey(1);
			if (config==null) {
				resultInfo.setCode("-1");
				resultInfo.setMessage("系统配置为空");
				return resultInfo;
			}
			
			if (StringUtils.isEmpty(config.getSwtcAddress())||StringUtils.isEmpty(config.getSwtcSecret())) {
				resultInfo.setCode("-1");
				resultInfo.setMessage("井通地址和私钥未配置");
				return resultInfo;
			}
			
			//遍历
			for (String id : ids) {
				try {
					UserTask userTask = userTaskMapper.selectByPrimaryKey(Integer.parseInt(id));
					//如果奖励不存在获取不是处于未发发放的状态
					if (userTask==null||!userTask.getStatus().equals(3)) {
						continue;
					}
					
					//如果是墨客，直接改变状态
					if (userTask.getActivated().equals(1)) {
						userTask.setStatus(2);  //待领取
						userTask.setDeleted(0);  //设置未转账的状态
						//获取该用户的墨客钱包的地址
						Wallet wallet = walletMapper.selectWallet(userTask.getUserId(),1);
						//如果钱包为null，不转账
						if (wallet==null) { 
							continue;
						}
					}else {  //如果是井通，需要发放奖励
						userTask.setStatus(2);  //待领取
						//获取该用户的井通钱包的地址
						Wallet wallet = walletMapper.selectWallet(userTask.getUserId(),2);
						//如果钱包为null，不转账
						if (wallet==null) { 
							continue;
						}
						
						//钱包存在，配置存在，那么可以进行转账了
						PayNeedData data=new PayNeedData();
						String orderNum=System.currentTimeMillis()+"";
						data.setClient_id(orderNum);  //订单号
						data.setSecret(Base64Util.Decoder(config.getSwtcSecret()));  //秘钥
						
						PayObject payObject=new PayObject();
						payObject.setDestination(wallet.getAddress());   //目标账号
						payObject.setSource(config.getSwtcAddress());  //发起账号
						
						//转账的金额
						Amount amount=new Amount();
						amount.setValue(String.valueOf(userTask.getMoney()));  //设置转账的金额
						
						payObject.setAmount(amount);
						
						data.setPayment(payObject);
						
						
						//调用转账接口
						Map<String, Object> map=SwtcUtils.pay(data, config.getSwtcAddress());
						
						//转账失败
						if (map==null) {
							throw new GlobalException("-1", "奖励Id为"+id+"的奖励领取失败");
						}
						
						if (!(Boolean)map.get("success")==true||!(map.get("result").equals("tesSUCCESS"))) {
							throw new GlobalException("-1", "奖励Id为"+id+"的奖励领取失败");
						}
						
						String swtHash=(String) map.get("hash");  //获取hash值，用于查询交易信息
						
						//添加一条钱包明细
						
						//封装信息
						WalletDetail detail=new WalletDetail();
						detail.setHash(swtHash);  //hash的值
						detail.setNumber(orderNum);   //交易号
						detail.setCreateDate(new Date());  //设置创建日期
						detail.setBlock(null);   //设置区块信息
						detail.setFromAddress(config.getSwtcAddress());  //转账方的钱包地址
						detail.setToAddress(wallet.getAddress());
						detail.setFee((Double)map.get("fee"));   //旷工费用
						detail.setRemark("任务奖励发放");   //设置备注
						detail.setTransactionDate(DateUtil.getDate("yyyy-MM-dd HH:mm:ss"));  //交易时间
						detail.setActivated(wallet.getType());  //设置钱包的类型
						detail.setMoney("+"+userTask.getMoney());
						detail.setWalletId(wallet.getWalletId());
						
						int count=detailMapper.insertSelective(detail);
						if (count==0) {
							throw new GlobalException("-1","奖励Id为"+id+"的任务，钱包明细插入失败");
						}
					}
					
					//更新奖励的状态
					int count = userTaskMapper.updateByPrimaryKeySelective(userTask);
					if (count==0) {
						throw new GlobalException("-1","奖励Id为"+id+"的任务，更新奖励状态失败");
					}
					
				} catch (Exception e) {
					throw new GlobalException("-1", "奖励Id为"+id+"发放奖励失败");
				}
			}
		}else {  //奖励驳回
			for (String id : ids) {
				UserTask userTask = userTaskMapper.selectByPrimaryKey(Integer.parseInt(id));
				//如果奖励不存在或者不是处于未发发放的状态,那不能驳回
				if (userTask==null||!userTask.getStatus().equals(3)) {
					continue;
				}
				
				userTask.setStatus(4);
				int count = userTaskMapper.updateByPrimaryKeySelective(userTask);
				if (count==0) {
					resultInfo.setCode("-1");
					resultInfo.setMessage("驳回失败");
					return resultInfo;
				}
				
			}
		}
		
		resultInfo.setMessage("操作成功");
		return resultInfo;
	}

	@Override
	public Object getTaskById(Integer detailId) {
		ResultInfo resultInfo=new ResultInfo();
		
		UserTask userTask = userTaskMapper.selectByPrimaryKey(detailId);
		
		if (userTask==null) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("该任务信息不存在");
			return resultInfo;
		}
		
		
		//获取任务详情
		Task task = taskMapper.selectByPrimaryKey(userTask.getTaskId());
		
		if (task==null) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("该任务不存在");
			return resultInfo;
		}
		
		
		//获取用户信息
		User user = userMapper.selectByPrimaryKey(userTask.getUserId());
		
		if (user==null) {
			resultInfo.setCode("-1");
			resultInfo.setMessage("该用户不存在");
			return resultInfo;
		}
		
		
		//根据不同的类别返回信息
		userTaskDetailVos detailVos=new userTaskDetailVos();
		detailVos.setContent(task.getTaskName());  //任务名称
		detailVos.setType(task.getTaskTypeId());  //任务类型
		detailVos.setIntroduction(task.getIntroduction()); //任务简介
		detailVos.setCreateDate(userTask.getCreateDate());  //完成时间
		detailVos.setStatus(userTask.getStatus()); //发放状态
		detailVos.setActivated(task.getActivated()); //奖励类型
		detailVos.setMoeny(task.getRewardMoney());  //奖励的钱
		detailVos.setUserId(user.getUserId());
		detailVos.setMobile(user.getMobile());
		
		//如果是分享类
		if (task.getTaskTypeId().equals(4)) {
			//需要获取被邀请人的信息
			User u = userMapper.selectByPrimaryKey(userTask.getRecommendId());
			if (u!=null) {
				detailVos.setRecommendDate(u.getCreateDate());
				detailVos.setRecommendId(userTask.getRecommendId());
				detailVos.setRecommendMobile(u.getMobile());
			}
			
		}else if (task.getTaskTypeId().equals(1)){  //下载安装类
			detailVos.setAppId(task.getAppId());
			
		}else if (task.getTaskTypeId().equals(6)){  //签到类
			
		}
		
		resultInfo.setResult(detailVos);
		resultInfo.setMessage("获取成功");
		return resultInfo;
	}

	@Override
	public Object getTaskDetailByUserId(Integer userId) {
		ResultInfo resultInfo=new ResultInfo();
		List<UserTask> userTasks = userTaskMapper.selectUserTaskList(userId);
		resultInfo.setResult(userTasks);
		resultInfo.setMessage("获取成功");
		return resultInfo;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

}
